﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class UIKDANSP : ViewBase
    {  

        public UIKDANSP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;   
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            
            if (dwMaster == null)
            {
                KEY="";
            }
            else
            {
                KEY = dwMaster["KDSKUNDNR"].ToString() + dwMaster["KDSANSPNA"].ToString();  
            }
        }
       
        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            { 
                case "KDDEL":
                    KDDEL(msg);
                    break;

            }
        }

        public override void New()
        {

            if (!IsNew)
            {
                IsNew = true;
                return;
            }
            if (NNAME.Trim().Length == 0)
            {
                MessageBox.Show("请先输入姓名");
                return;
            }
            dwData = TBMain.NewRow();
            ((VKDANSP)MV).SetUpNewRow(ref dwData);
            dwData["KDSKUNDNR"] = KUNDNR;
            dwData["KDSANSPNA"] = NNAME.PadRight(8);
            dwData["KDSNEUDAT"] = MV.date;
            dwData["KDSAENDBEN"] = app.User;
            dwData["KDSANREDE"] = "2"; 
            ((VKDANSP)MV).InsertKDANSP(dwData, app.company);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
        }

       
        public override void Cancel()
        {
            string _KDKUNDNR = dwMaster["KDKUNDNR"].ToString();
            string filter = "KDSKUNDNR='" + dwMaster["KDSKUNDNR"].ToString() + "' AND KDSANSPNA='" + dwMaster["KDSANSPNA"].ToString() + "'";
            vwMaster.View.MoveCurrentTo(-1);
            DataRow[] rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
        }

        public void KDDEL(MessageInfo msg)
        {
            dwData = TBMain.NewRow();
            dwData["KDMANDANT"] = app.Mandant;
            dwData["KDSKUNDNR"] = msg.P1;
            ((VKDANSP)MV).DeleteKDANSP(dwMaster, 2);

        }

         

        public override void Save()
        {
            if (dwMaster == null)
                return;          
            ((VKDANSP)MV).UpdateKDANSP(dwMaster, app.company);
        }

        public override void Copy()
        {
            base.Copy();

        }

     

        public override void CancelCommand(object sender, RoutedEventArgs e)
        {
            base.CancelCommand(sender, e);

        }

        
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            Copy();
            base.CopyCommand(sender, e);
        }

        public virtual void OnKUNDNRChg(string value)
        {
        }
    }
    public partial class UIKDANSP : ViewBase
    {
        public VKDANSP TMV
        {
            get
            {
                return (VKDANSP)MV;
            }
        }

        #region ==NNAME===========
        public static readonly DependencyProperty NNAMEProperty = DependencyProperty.Register("NNAME",
            typeof(string), typeof(UIKDANSP),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnNNAMEChanged));
        public string NNAME
        {
            get
            {
                return (string)GetValue(NNAMEProperty);
            }
            set
            {
                SetValue(NNAMEProperty, value);
            }
        }
        private static void OnNNAMEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIKDANSP;
            if (src != null)
            {
                src.NNAME = (string)e.NewValue;
            }
        }
        #endregion

        #region ==KUNDNR===========
        public static readonly DependencyProperty KUNDNRProperty = DependencyProperty.Register("KUNDNR",
            typeof(string), typeof(UIKDANSP),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKUNDNRChanged));
        public string KUNDNR
        {
            get
            {
                return (string)GetValue(KUNDNRProperty);
            }
            set
            {
                SetValue(KUNDNRProperty, value);
                OnKUNDNRChg(value);
            }
        }
        private static void OnKUNDNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIKDANSP;
            if (src != null)
            {
                src.KUNDNR = (string)e.NewValue;
            }
        }
        #endregion
    }
}
